package com.maszy.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.maszy.bean.Action;
import com.maszy.db.DBConnection;

public class ActionService {
	Action service=new Action(); 
	private static PreparedStatement pst;
	private static ResultSet rs = null;
	public  int add(Action obj) {
		DBConnection con = new DBConnection();
		int result = 0;
		String sql ="insert into Maszy_Action(Maszy_Action_Id,Maszy_Action_Name,Maszy_Action_Remark) values(?,?,?)";
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {			
			pst.setLong(1, obj.getAction_Id());
			pst.setString(2,obj.getAction_Name());
			pst.setString(3, obj.getAction_Remark());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return result;
	}
	public int update(Action obj){
		DBConnection con = new DBConnection();
		int i=0;
		String sql="update Maszy_Action set Maszy_Action_Name=?,Maszy_Action_Remark=? where Maszy_Action_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);			
			pst.setString(1, obj.getAction_Name());
			pst.setString(2, obj.getAction_Remark());
			pst.setLong(3, obj.getAction_Id());
			i=pst.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}
	public int delete(Action obj){
		DBConnection con = new DBConnection();
		int i=0;
		String sql="delete from  Maszy_Action  where Maszy_Action_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);	
			pst.setLong(1, obj.getAction_Id());
			i=pst.executeUpdate();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}
	public Action queryById(Action obj){
		DBConnection con = new DBConnection();
		String sql="select * from Maszy_Action where Maszy_Action_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);	
			pst.setLong(1, obj.getAction_Id());
			rs = pst.executeQuery();
			while (rs.next()) {
				obj=new Action();
				obj.setAction_Id(rs.getLong("Maszy_Action_Id"));
				obj.setAction_Name(rs.getString("Maszy_Action_Name"));
				obj.setAction_Remark(rs.getString("Maszy_Action_Remark"));	
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return obj;
	}
	public List<Action> queryAll(){
		List<Action> list=new ArrayList<Action>();
		DBConnection con = new DBConnection();
		String sql="select * from Maszy_Action";
		try {
			pst = con.getConnection().prepareStatement(sql);				
			rs = pst.executeQuery();
	
			while (rs.next()) {
				Action obj=new Action();
				obj.setAction_Id(rs.getLong("Maszy_Action_Id"));
				obj.setAction_Name(rs.getString("Maszy_Action_Name"));
				obj.setAction_Remark(rs.getString("Maszy_Action_Remark"));	
				list.add(obj);
			}
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return list;
	}
}
